<?php

namespace app\admin\controller\shop;

use app\admin\model\money\Platform;
use app\admin\model\Shoporderprofit;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\api\service\VbillService;
use app\common\controller\Backend;

/**
 * 店铺充值管理
 *
 * @icon fa fa-circle-o
 */
class Shopcharge extends Backend
{

    /**
     * Shopcharge模型对象
     * @var \app\admin\model\Shopcharge
     */
    protected $model = null;
    protected $relationSearch = true;
    protected $noNeedRight = ['refund'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shopcharge;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['shop','user','charge'])
                ->where($where)
                ->where('Shopcharge.status','<>','paying')
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {

                $list[$k]['desc'] = "";
                if($v['charge'] != null){
                    $list[$k]['desc'] = "充".$v['charge']['amount']."得".$v['charge']['get_amount'];
                }


            }
            $totalAmount = $this->model->where('status','not in',['paying','refund'])->sum('amount');
            $todayAmount = $this->model->whereTime("create_time",'today')->where('status','not in',['paying','refund'])->sum('amount');

            $result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['one' => $totalAmount, 'two' => $todayAmount]);


            return json($result);
        }
        return $this->view->fetch();
    }

    public function refund()
    {
        $id = $this->request->param('id');

        $shopChargeModel = new \app\admin\model\Shopcharge();
        $charge = $shopChargeModel->where('id',$id)->find();
        if($charge['status'] == 'refund'){
            $this->error("此订单已退款无需发起操作");
        }
        $req = array();
        $req['mno']=$this->mno;
        $req['ordNo']= getOrderSn();
        $req['origOrderNo']= $charge['order_no'];
        $req['amt']= $charge['amount'];

        $vbillService = new VbillService();
        $ret = $vbillService->refund($req);
        $result = json_decode($ret,true);
        if($result['respData']['bizCode'] != "0000"){
            $this->error($result['respData']['bizMsg']);
        }
        $userBalanceModel = new Userbalance();

        $userBalanceModel->where('type','in')->where('order_id',$id)->delete();
        $shopOrderProfitModel = new Shoporderprofit();
        $shopOrderProfitModel->where('type','charge')->where('order_id',$id)->delete();

        $userOrderProfitModel = new Userorderprofit();
        $userOrderProfitModel->where('type','charge')->where('order_id',$id)->delete();

        $shopChargeModel->where('id',$charge['id'])->update(['status'=>'refund']);

        $platformModel = new Platform();
        $platformModel->where('order_id', $charge['id'])->where('type', ' charge')->where('from', 'qp')->delete();


        $this->success('退款成功');

    }

}
